package edu.base.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.base.dao.UsersMapper;
import edu.base.model.Statistics;
import edu.base.model.Users;

@Service
public class UsersService {

	@Autowired
	UsersMapper usersMapper;
	
	
	public Integer insert(Users item) {
		if(item.getCreateDate() == null) {
			item.setCreateDate(new Date());
		}
		return usersMapper.insertSelective(item);
	}
	
	public void del(Integer id) {
		usersMapper.deleteByPrimaryKey(id);
	}
	
	public void update(Users item) {
		usersMapper.updateByPrimaryKeySelective(item);
	}
	
	public Users findByKey(Integer key) {
		return usersMapper.selectByPrimaryKey(key);
	}
	
	public Users findByLoginName(String name) {
		return usersMapper.findByLoginName(name);
	}
	public Integer findByTel(String tel){
		return usersMapper.findByTel(tel);
	}
	public List<Users> query(Integer type, String name, String tel, Integer p, Integer size){
		if(p==null) {
			p = 1;
		}
		if(name != null && name.length() > 0) {
			name = "%" + name + "%";
		}else {
			name=null;
		}
		if(tel != null && tel.length() > 0) {
			tel = "%" + tel + "%";
		}else {
			tel = null;
		}
		return usersMapper.query(type, name,tel, (p-1)*size, size);
	}
	
	public Integer count(Integer type,String name, String tel) {
		if(name != null && name.length() > 0) {
			name = "%" + name + "%";
		}else {
			name=null;
		}
		if(tel != null && tel.length() > 0) {
			tel = "%" + tel + "%";
		}else {
			tel = null;
		}
		return usersMapper.count(type, name,tel);
	}
	
	
	
	public List<Statistics> stat(Integer year){
		return usersMapper.stat(year);
	}
	
}
